<?php
namespace Home\Model;
use Think\Model;
use Common\Library\Category;
class CategoryModel extends Model{
	public function getCategory($category_id){
		$map=array('category_id'=>(int)$category_id);
		$category=$this->where($map)->find();
		$map['language_id']=get_default_lng_id();
		$cd=M('CategoryDescription')->where($map)->find();
		$category['category_description']=$cd;
		return $category;
	}
	/**
	 * 获取分类列表
	 */
	public function getCategories(){		
		$map=array(
			'language_id'	=>	get_default_lng_id(),
			'status'		=>	1
			);
		$order='sort_order ASC';		
		$categories=$this->join('LEFT JOIN  __CATEGORY_DESCRIPTION__ ON __CATEGORY__.category_id=__CATEGORY_DESCRIPTION__.category_id')->where($map)->field(C('DB_PREFIX')."category.category_id,title,parent_id,image,status")->order($order)->select();
		foreach ($categories as $k => $v) {
			$categories[$k]['url']	=	U('Product/category?category_id='.$v['category_id']);
		}
		return $categories;
	}
	/**
	 * 根据ID获取分类列表
	 */

	public function getCategoriesById($category_id=0){	
		$map=array(
			'language_id'	=>	get_default_lng_id(),
			'status'		=>	1,
			'parent_id'		=>	$category_id
			);
		$order='sort_order ASC';		
		$categories=$this->join('LEFT JOIN  __CATEGORY_DESCRIPTION__ ON __CATEGORY__.category_id=__CATEGORY_DESCRIPTION__.category_id')->where($map)->field(C('DB_PREFIX')."category.category_id,title,parent_id,image,status")->order($order)->select();
		return $categories;
	}	

	public function getChildId($category_id){
		$cate=$this->select();
		$cate_ids=Category::getChildsId ($cate, $category_id,"category_id","parent_id");
		$cate_ids=array_merge(array($category_id),$cate_ids);		
		return $cate_ids;
	}

	
}